<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 导入mapbox依赖 -->
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        #mouse-position {
            position: fixed;
            z-index: 1000;
            top: 10px;
            left: 50px;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="mouse-position">
    </div>
    <div id="map">

    </div>
    <script>
        /* 3、实例化地图 */
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdiZW5jaGFvIiwiYSI6ImNsODU3aGRiODA0Y2UzcHBzZmFlcmdqZ2sifQ.8k59W_pB_Riwe6o-MneRlA'
        const map = new mapboxgl.Map({
            container: "map",
            /* style-layers */
            style: "mapbox://styles/mapbox/streets-v12",
            center: [114.30, 30.50]
        })
        /* 地图控件 */
        map.addControl(new mapboxgl.FullscreenControl());
        const nav = new mapboxgl.NavigationControl(
            {
                //是否显示指南针按钮，默认为true
                "showCompass": true,
                //是否显示缩放按钮，默认为true
                "showZoom": true
            }
        );
        //添加导航控件，控件的位置包括'top-left', 'top-right','bottom-left' ,'bottom-right'四种，默认为'top-right'
        map.addControl(nav, 'top-left');

        map.addControl(new mapboxgl.AttributionControl({
            //是否折叠属性信息
            compact: true,
            //自定义属性信息
            customAttribution: ["中地数码", "GIS高校论坛"]
        }));
        const scale = new mapboxgl.ScaleControl({
            unit: 'metric|imperial' //默认为公里
        })
        map.addControl(scale);
        map.on('mousemove', function (e) {
            const { lng, lat } = e.lngLat;
            document.getElementById('mouse-position').innerHTML = `
   经度:${lng.toFixed(2)},纬度:${lat.toFixed(2)}
  `
        });
    </script>
</body>

</html>